﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using DatabaseModel;
using GuiSearcher.Model;
using GuiSearcher.Enums;
using System.ComponentModel;


namespace GuiSearcher.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase {

        #region Constants

        /// <summary>
        /// The <see cref="LogBookEntries" /> property's name.
        /// </summary>
        public const string LogBookEntriesPropertyName = "LogBookEntries";

        /// <summary>
        /// The <see cref="LogBookCountString" /> property's name.
        /// </summary>
        public const string LogBookCountStringPropertyName = "LogBookCountString";

        #endregion


        #region Private Fields

        private List<LogBookEntry> _logBookEntries = new List<LogBookEntry>();
        private RelayCommand _searchCommand;
        private Dictionary<string, SearchModeEnum> _searchModes = new Dictionary<string, SearchModeEnum>();
        private RelayCommand _resetValues;
        private string _logBookCountString = "Count:";


        #endregion


        #region Constructors

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel() {
            SearchCriteria = new SearchCriteria();
        }



        #endregion


        #region Properties


        /// <summary>
        /// Sets and gets the LogBookCountString property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string LogBookCountString {
            get {
                return _logBookCountString;
            }

            set {
                if (_logBookCountString == value) {
                    return;
                }

                RaisePropertyChanging(LogBookCountStringPropertyName);
                _logBookCountString = value;
                RaisePropertyChanged(LogBookCountStringPropertyName);
            }
        }

        

        public SearchCriteria SearchCriteria {
            get;
            set;
        }

        public Dictionary<SearchModeEnum, string> SearchModes {
            get {
                return SearchModeMapper.SearchModes;
            }
        }


        /// <summary>
        /// Gets the SearchCommand.
        /// </summary>
        public RelayCommand SearchCommand {

            get {
                return _searchCommand 
                    ?? (_searchCommand = new RelayCommand(ExecuteSearchCommand));
            }
        }

        


        /// <summary>
        /// Sets and gets the LogBookEntries property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the MessengerInstance when it changes.
        /// </summary>
        public List<LogBookEntry> LogBookEntries {
            get {
                return _logBookEntries;
            }

            set {
                if (_logBookEntries == value) {
                    return;
                }

                RaisePropertyChanging(LogBookEntriesPropertyName);
                var oldValue = _logBookEntries;
                _logBookEntries = value;
                RaisePropertyChanged(LogBookEntriesPropertyName, oldValue, value, true);
                
            }
        }

        /// <summary>
        /// Gets the ResetValues.
        /// </summary>
        public RelayCommand ResetValues {
            get {
                return _resetValues 
                    ?? (_resetValues = new RelayCommand(ExecuteResetValues));
            }
        }


        #endregion


        #region Public Methods


        #endregion


        #region Private Methods

        private void ExecuteSearchCommand() {
            LogBookEntries = LogBookEntryReporitory.GetListBySearchCriteria(SearchCriteria);
            LogBookCountString = string.Format("Count: {0}", LogBookEntries.Count);
        }


        private void ExecuteResetValues() {
            SearchCriteria.Reset();
        }

        #endregion

        




    }
}